package self.security.entity;

import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component;
import self.enumerate.ResponseCode;
import self.result.R;
import self.security.utils.HttpResponseUtils;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Component
public class UnAuthEntryPoint implements AuthenticationEntryPoint {

    @Override
    public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException {
        //在这里需要直接返回JSON格式告诉他没有权限访问
        R error = R.error(ResponseCode.UNAUTHORIZATION_EXCEPTION.getCode(),
                ResponseCode.UNAUTHORIZATION_EXCEPTION.getMessage());
        //接下来将数据直接返回
        HttpResponseUtils.sendResponse(response,error);
    }
}
